/**
 * 
 */
package com.alphasystem.commerce.common.util;

import static java.util.Calendar.*;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * @author sali
 * 
 */
public class DateUtil {

	public static Date copyData(Date src) {
		if (src == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(src);

		Calendar tCal = Calendar.getInstance();
		tCal.set(YEAR, cal.get(YEAR));
		tCal.set(MONTH, cal.get(MONTH));
		tCal.set(DATE, cal.get(DATE));
		tCal.set(HOUR, cal.get(HOUR));
		tCal.set(MINUTE, cal.get(MINUTE));
		tCal.set(SECOND, cal.get(SECOND));
		tCal.set(MILLISECOND, cal.get(MILLISECOND));
		return tCal.getTime();
	}

	public static GregorianCalendar getGregorianCalendar(Date src) {
		GregorianCalendar cal = null;
		if (src != null) {
			cal = new GregorianCalendar();
			cal.setTime(src);
		}
		return cal;
	}

	public static XMLGregorianCalendar getXMLGregorianCalendar(Date src) {
		if (src != null) {
			try {
				return DatatypeFactory.newInstance().newXMLGregorianCalendar(
						DateUtil.getGregorianCalendar(src));
			} catch (DatatypeConfigurationException e) {
			}
		}
		return null;
	}
}
